package com.zk.util;

import java.util.List;
import java.util.Random;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.cluster.loadbalance.AbstractLoadBalance;

/**
 * 自定义负载均衡
 * @author Administrator
 *
 */
public class MyLoadBalance extends AbstractLoadBalance{

	
	private final Random random = new Random();

	@Override
	protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
		int length = invokers.size(); // Number of invokers
		int totalWeight = 0; // The sum of weights
		
      
	      for (int i = 0; i < length; i++) {
	          int weight = getWeight(invokers.get(i), invocation);
	          System.err.println("权重："+weight);
	          totalWeight += weight; // Sum
	         
	      }
	    System.err.println("地址"+url);
		
		System.err.println("节点服务数量："+length);
		System.err.println("所有权重："+totalWeight);
		int num = random.nextInt(length);
		System.err.println("******************自定义过滤器**********************"+num);
		System.err.println("数据"+invocation);
		return invokers.get(num);
	}	 


}
